package com.tobeing.picture.picker.customs;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.tobeing.picture.picker.R;
import com.tobeing.picture.picker.adapters.FolderAdapter;
import com.tobeing.picture.picker.models.Folder;

import java.util.List;

/**
 * Created by Administrator on 2017/11/16/016.
 */

public class FolderWin extends PopupWindow {
    private Context mContext;
    private View mRootView;
    private ListView mLVFolder;

    private List<Folder> mFolders;
    private FolderClickListener mFolderClickListener;

    public FolderWin(Context context, List<Folder> folders){
        mContext = context;
        mFolders = folders;
        mRootView = LayoutInflater.from(mContext).inflate(R.layout.folders_win, null, false);
        mLVFolder = mRootView.findViewById(R.id.lv_folder);
        setContentView(mRootView);
        setWidth(mContext.getResources().getDisplayMetrics().widthPixels);
        setHeight((int) (mContext.getResources().getDisplayMetrics().heightPixels * 0.5));
        setTouchable(true);
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable());
        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction()== MotionEvent.ACTION_OUTSIDE){
                    dismiss();
                    return true;
                }
                return false;
            }
        });
        mLVFolder.setAdapter(new FolderAdapter(mContext, folders));
        mLVFolder.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dismiss();
                if(mFolderClickListener != null){
                    mFolderClickListener.onClick(mFolders.get(position));
                }
            }
        });
    }

    public void setFolderClickListener(FolderClickListener listener){
        mFolderClickListener = listener;
    }
}
